﻿package model.poker;

/**
 * 洗牌
 * @author qykings
 *
 */
import java.util.ArrayList;
import java.util.Random;

import util.KPrintInfo;

public class ShufflePoker {
	private ArrayList<Poker>	pokerArrayList	= new ArrayList<Poker>();
	
	// 黑桃
	private ArrayList<Poker>	blackList		= new ArrayList<Poker>();
	
	// 草花
	private ArrayList<Poker>	flowerList		= new ArrayList<Poker>();
	
	// 红桃
	private ArrayList<Poker>	redList			= new ArrayList<Poker>();
	
	// 方片
	private ArrayList<Poker>	boxList			= new ArrayList<Poker>();
	
	public ArrayList<Poker> getPokerArr() {
		initShuffle();
		return putArr();
	}
	
	private void initShuffle() {
		
		try {
			/**
			 * 黑桃 草花 红桃 方片
			 * 
			 * 1 2 3 4
			 */
			blackList = Shuffle(1);
			flowerList = Shuffle(2);
			redList = Shuffle(3);
			boxList = Shuffle(4);
		} catch (Exception e) {
			KPrintInfo.println(e);
		}
		
	}
	
	private ArrayList<Poker> listSum(ArrayList<Poker> targetList, ArrayList<Poker> list) {
		for (int i = 0; i < list.size(); i++) {
			targetList.add(list.get(i));
		}
		return targetList;
	}
	
	private ArrayList<Poker> putArr() {
		
		ArrayList<Poker> arrayList = new ArrayList<Poker>();
		listSum(arrayList, blackList);
		listSum(arrayList, flowerList);
		listSum(arrayList, redList);
		listSum(arrayList, boxList);
		return RandomPosition(arrayList);
	}
	
	/**
	 * 黑桃 草花 红桃 方片
	 * 
	 * 1 2 3 4 生成牌
	 */
	private ArrayList<Poker> Shuffle(int color) {
		
		ArrayList<Poker> returnList = new ArrayList<Poker>();
		for (int j = 1; j < 14; j++) {
			Poker poker = new Poker();
			poker.setValue(j);
			poker.setColor(color);
			returnList.add(poker);
		}
		
		// 1 2 3 4 5 6 7 8 9 10 11 12 13
		
		// 打乱数据下标的方式来随机
		
		
		return RandomPosition(returnList);
		
	}
	
	private ArrayList<Poker> RandomPosition(ArrayList<Poker> _list) {
		// 随机种子
		int randomNum = _list.size() - 1;
		
		for (int i = 0; i < _list.size(); i++) {
			int position = getRandomNum(randomNum);
			Poker tempValue = _list.get(position);
			_list.remove(position);
			_list.add(tempValue);
		}
		return _list;
	}
	
	/**
	 * 
	 * @param v
	 *            随机的范围
	 * @return
	 */
	private int getRandomNum(int v) {
		Random random = new Random();
		int mun = random.nextInt(v) + 1;
		return mun;
	}
}
